package com.example.myapplication.ui.study

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication.databinding.BookCateBinding
//直接引用对应的xml 的Binding 即可
import com.example.myapplication.databinding.StudyHomeBinding

class StudyFragment : Fragment() {
    private var _binding:StudyHomeBinding?=null
    private val binding get()=_binding!!  //表示不可能为空

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homefragViewModel =
            ViewModelProvider(this).get(StudyViewModel::class.java)
        _binding = StudyHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root
        binding.recyclerView.layoutManager=
            LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        binding.recyclerView.adapter=FruitAdapter()
        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    class FruitAdapter : RecyclerView.Adapter<FruitAdapter.ViewHolder>() {
        inner class ViewHolder(binding: BookCateBinding) : RecyclerView.ViewHolder(binding.root) {
            val itembook = binding.book
            val itemname = binding.name
            val itemisfree = binding.isfree
            val itemprocess = binding.process

        }
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            //重写 本类中的binding
            val binding = BookCateBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            return ViewHolder(binding)
        }
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            if(position==0) {
                holder.itembook.setImageResource(R.drawable.icon_large1)
                holder.itemname.text = "Android Multidex原理与实现"
                holder.itemisfree.text = "免费课程"
                holder.itemprocess.text = "医学20%"
            }
            else if(position==1)
            {
                holder.itembook.setImageResource(R.drawable.icon_large2)
                holder.itemname.text="Android Multidex原理与实现"
                holder.itemisfree.text="免费课程"
                holder.itemprocess.text="医学20%"
            }
            else if(position==2)
            {
                holder.itembook.setImageResource(R.drawable.icon_large3)
                holder.itemname.text="Android Multidex原理与实现"
                holder.itemisfree.text="免费课程"
                holder.itemprocess.text="医学20%"
            }
            else
            {
                    holder.itembook.setImageResource(R.drawable.icon_large4)
                holder.itemname.text="Android Multidex原理与实现"
                holder.itemisfree.text="免费课程"
                holder.itemprocess.text="医学20%"
            }
        }
        override fun getItemCount(): Int {
            return 4
        }
    }
}